package com.lagou.homework;

import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class NumberBean implements WritableComparable<NumberBean> {
    //从输入文件中读取到的某个数字
    private Integer number;
    //排序之后的顺序编号, 在reduce方法中设置
    private Integer id = 0;

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(number);
        out.writeInt(id);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.number = in.readInt();
        this.id = in.readInt();
    }


    @Override
    public int compareTo(NumberBean o) {
        //compare方法, 按升序排序
        if (this.getNumber() - o.getNumber() > 0) {
            return 1;
        } else if (this.getNumber() - o.getNumber() < 0) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public String toString() {
        //输出id和数字, 以制表符分隔
        return "" + id + "\t" + number;
    }

    public Integer getNumber() {
        return number;
    }

    public NumberBean() {
    }

    public NumberBean(Integer number, Integer id) {
        this.number = number;
        this.id = id;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}
